home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / com / internet / stik / dial_085 / rem_ctrl / req_dial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-28  |  7.1 KB  |  233 lines

  1.  
  2.  
  3. #include <aes.h>
  4. #include <tos.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10.  
  11. #include "transprt.h"
  12. #include "remctrl.h"
  13.  
  14.  
  15. #define  DIAL_SCRIPT        "DIAL.SCR"
  16. #define  DIAL_MAX_NUMBER    5
  17. #define  TIMEOUT            60
  18.  
  19.  
  20. void  gem_program (void),  do_some_work (void);
  21. long  get_stik_cookie (void);
  22.  
  23.  
  24. typedef int  BOOLEAN;
  25.  
  26. DRV_LIST   *stik_drivers;
  27. TPL        *tpl;
  28. char       alert[200];
  29. int        gl_apid;
  30. int        errors[] = {  9, 
  31.                          IP_USER_ABORT, IP_IN_PROGRESS, IP_IS_CONNECTED,
  32.                          IP_SCR_NOT_FOUND, IP_MAX_EXCEEDED, IP_FATAL_PROBLEM,
  33.                          IP_OPEN_FAILED, IP_PORT_LOCKED
  34.                       };
  35.  
  36. char  not_there[] = "[1][ |  STiK is not loaded or enabled !   ][ Hmmm ]";
  37. char  corrupted[] = "[1][ |  STiK structures corrupted !   ][ Oooops ]";
  38. char  found_it[]  = "[3][ |  Driver \'%s\',|  by %s, found,   |  version %s.][ Okay ]";
  39. char  no_module[] = "[1][ |  STiK Transport Driver not found !   ][ Grmbl ]";
  40. char  no_dialer[] = "[1][ |  STiK variable DIALER not set !   ][ Grrr ]";
  41. char  not_start[] = "[1][ |  Dialer is not running !   ][ Forgot ]";
  42. char  begin_it[]  = "[3][ |  Dialer found. Sending dial request.   ][ Good ]";
  43. char  waiting[]   = "[3][ |  Waiting %d seconds for a response.   ][ Do it ]";
  44. char  dial_error_text[][70] = { "",
  45.                     "[1][ |  Dial error :| |    User aborted dialing !   ][ Uhmmm ]",
  46.                     "[1][ |  Dial error :| |    Dialing already in progress !   ][ Uhmmm ]",
  47.                     "[1][ |  Dial error :| |    Already connected !   ][ Uhmmm ]",
  48.                     "[1][ |  Dial error :| |    Script not found !   ][ Uhmmm ]",
  49.                     "[1][ |  Dial error :| |    Redials exceed max number !   ][ Uhmmm ]",
  50.                     "[1][ |  Dial error :| |    Modem reports fatal problem !   ][ Uhmmm ]",
  51.                     "[1][ |  Dial error :| |    Can't pass port to STiK !   ][ Uhmmm ]",
  52.                     "[1][ |  Dial error :| |    Can't open serial port !   ][ Uhmmm ]"
  53.                };
  54. char  success[]   = "[3][ |  Dialing done. Connection established.   ][ Well done ]";
  55. char  other_err[] = "[1][ |  Dialer reports an error.   | |  Code unknown !][ Hey ]";
  56. char  timer_qu[]  = "[2][ |  Wait timeout expired.| |    Give it some more time ?   ][ Yes | Abort ]";
  57. char  no_rslv[]   = "[1][ |  Attempt to resolve hostname   | |  failed !][ Piss off ]";
  58. char  bad_ip[]    = "[1][ |  Wrong format in IP address !  ][ F... ]";
  59. char  no_udp[]    = "[1][ |  Couldn't open UDP connection !  ][ Hmm ]";
  60.  
  61.  
  62.  
  63. void  main()
  64.  
  65. {
  66.    gl_apid = appl_init();
  67.  
  68.    gem_program();
  69.  
  70.    appl_exit();
  71.  }
  72.  
  73.  
  74. void  gem_program()
  75.  
  76. {
  77.    stik_drivers = (DRV_LIST *) Supexec (get_stik_cookie);
  78.  
  79.    if (stik_drivers == 0L) {
  80.         form_alert (1, not_there);
  81.         return;
  82.       }
  83.    if (strcmp (stik_drivers->magic, MAGIC) != 0) {
  84.         form_alert (1, corrupted);
  85.         return;
  86.       }
  87.  
  88.    tpl = (TPL *) (*stik_drivers->get_dftab) (TRANSPORT_DRIVER);
  89.  
  90.    if (tpl != (TPL *) NULL) {
  91.         sprintf (alert, found_it, tpl->module, tpl->author, tpl->version);
  92.         form_alert (1, alert);
  93.         do_some_work();
  94.       }
  95.      else
  96.         form_alert (1, no_module);
  97.  }
  98.  
  99.  
  100. long  get_stik_cookie()
  101.  
  102. {
  103.    long  *work;
  104.  
  105.    for (work = * (long **) 0x5a0L; *work != 0L; work += 2)
  106.         if (*work == 'STiK')
  107.              return (*++work);
  108.  
  109.    return (0L);
  110.  }
  111.  
  112.  
  113. void  do_some_work()
  114.  
  115. {
  116.    BOOLEAN  use_udp;
  117.    uint32   rhost;
  118.    time_t   timeout;
  119.    int      count, handle, dialer_apid, message[8], abort, event, dummy;
  120.    char     *walk, dialer_name[64], dial_script_name[] = DIAL_SCRIPT;
  121.  
  122.    strncpy (dialer_name, getvstr ("DIALER"), 64);
  123.  
  124.    if (strcmp (dialer_name, "0") == 0) {
  125.         form_alert (1, no_dialer);
  126.         return;
  127.       }
  128.  
  129.    use_udp = (strchr (dialer_name, '.') != NULL);
  130.  
  131.    if (use_udp) {
  132.         if (atoi (dialer_name) == 0) {
  133.              if (resolve (dialer_name, NULL, &rhost, 1) <= 0) {
  134.                   form_alert (1, no_rslv);
  135.                   return;
  136.                 }
  137.            }
  138.           else {
  139.              for (count = 0, walk = dialer_name; count < 4; count++) {
  140.                   rhost = (rhost << 8) | atoi (walk);
  141.                   if ((walk = strchr (walk, '.') + 1) == (void *) 1L && count < 3) {
  142.                        form_alert (1, bad_ip);
  143.                        return;
  144.                      }
  145.                 }
  146.            }
  147.  
  148.         if ((handle = UDP_open (rhost, IP_DIALER_PORT)) < 0) {
  149.              form_alert (1, no_udp);
  150.              return;
  151.            }
  152.       }
  153.      else {
  154.         for (count = (int) strlen (dialer_name); count < 8; count++)
  155.              dialer_name[count] = ' ';
  156.  
  157.         for (count = 0; count < 8; count++)
  158.              dialer_name[count] = toupper (dialer_name[count]);
  159.  
  160.         dialer_name[8] = '\0';
  161.  
  162.         if ((dialer_apid = appl_find (dialer_name)) < 0) {
  163.              form_alert (1, not_start);
  164.              return;
  165.            }
  166.       }
  167.  
  168.    form_alert (1, begin_it);
  169.  
  170.    message[0] = IP_DIAL_REQUEST;
  171.    message[1] = gl_apid;
  172.    message[2] = 0;
  173.    *(char **) &message[3] = dial_script_name;
  174.    message[5] = DIAL_MAX_NUMBER;
  175.    for (count = 6; count < 8; count++)   message[count] = 0;
  176.  
  177.    if (use_udp) {
  178.         UDP_send (handle, (char *) message, 16);
  179.         UDP_send (handle, dial_script_name, (int) strlen (dial_script_name) + 1);
  180.       }
  181.      else
  182.         appl_write (dialer_apid, 16, message);
  183.  
  184.    sprintf (alert, waiting, (int) TIMEOUT);
  185.    form_alert (1, alert);
  186.  
  187.    timeout = time (NULL) + TIMEOUT;
  188.  
  189. /**/
  190. /*   Note that in case of IP_DIAL_ERROR : IP_OPEN_FAILED  a connection has been
  191. /*      established. If IP_DIAL_HANGUP is not used, your telephone bill will
  192. /*      start to grow ! ;-)
  193. /**/
  194.  
  195.    for (abort = 0; abort == 0;) {
  196.         event = evnt_multi (MU_MESAG | MU_TIMER, 0,0,0, 0,0,0,0,0, 0,0,0,0,0, message,
  197.                         500,0, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy);
  198.  
  199.         if (use_udp)
  200.              if (CNbyte_count (handle) > 0)
  201.                   if (CNget_block (handle, (char *) message, 16) == 16)
  202.                        event |= MU_MESAG;
  203.  
  204.         if (event & MU_MESAG)
  205.              switch (message[0]) {
  206.                 case AP_TERM :
  207.                   abort = 1;
  208.                   break;
  209.                 case IP_DIAL_DONE :
  210.                   form_alert (1, success);
  211.                   abort = 1;
  212.                   break;
  213.                 case IP_DIAL_ERROR :
  214.                   for (count = 1; count < errors[0]; count++)
  215.                        if (errors[count] == message[3])
  216.                             break;
  217.                   form_alert (1, (count == errors[0]) ? other_err : dial_error_text[count]);
  218.                   abort = 1;
  219.                   break;
  220.                 }
  221.  
  222.         if (time (NULL) > timeout) {
  223.              if (form_alert (1, timer_qu) == 2)
  224.                   abort = 1;
  225.                else
  226.                   timeout = time (NULL) + TIMEOUT;
  227.            }
  228.       }
  229.  
  230.    if (use_udp)
  231.         UDP_close (handle);
  232.  }
  233.